﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Ordering : System.Web.UI.Page
{

    public string OrderInfor { get; set; }
    private CartDao CartDao = new CartDao();
    private OrderDao OrderDao = new OrderDao();
    private Account account ;

    protected void Page_Load(object sender, EventArgs e)
    {
        // 验证当前是否有用户登录
        account = (Account)Session["account"];
        // 未登录则禁止访问本页，跳转到登录页面
        if (account == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {

                // 通过用户ID获取订单
                List<Model.Cart> list = CartDao.GetCartById(Convert.ToInt32(Request.Params["id"]), account.Id);

                ReflashPage(list);
        }
    }


    protected void BtnAddOrder_Click(object sender, EventArgs e) {
        BookInfo book = new BookInfo();
        book.Id = Convert.ToInt32(BookId.Text);

        Order order = new Order();
        order.Book = book;
        order.User = account;
        order.Count = Convert.ToInt32(Count.Text);
        order.Money = Convert.ToDouble(Money.Text);
        order.State = 2;



        int cartId = Convert.ToInt32(CartId.Text);

        if (OrderDao.AddNewOrder(order)&&CartDao.DeleteCartById(cartId))
        {
            Response.Redirect("Order.aspx");
        }
        else 
        {
            EmptyInfo.Text = "请求失败";
            //Response.Redirect("Cart.aspx");
        };
    }


    protected void ReflashPage(List<Model.Cart> list)
    {
        if (list != null)
        {
            // 套接字符串
            StringBuilder sbBuilder = new StringBuilder();
            System.Diagnostics.Debug.Write(list.Count());
            foreach (Model.Cart cart in list)
            {
                sbBuilder.AppendFormat(
                    "<div class ='page_bookDetail' >" +
                    "<div class='row'>" +
                    " <div class='col-md-3 clean'><img alt = '商品预览图' class='book_img' src='../Book_Image/{1}' /></div>" +
                        "<div class='col-md-8'><div class='row'><div class='col-md-offset-1'>" +
                        "<h2>《{2}》</h2><h4><font color='#D5D5D5'>推荐理由：</font>{3}</h4><hr class='bg-info' />" +
                                    "<blockquote class='bg-warning text-danger'>" +
                                       "<h3 class='text-primary'>金额：<font size = '5' color='#D9534F'>￥{4}</font ><font color='#D5D5D5' size='3'> x {5}</font></h3>" +
                                       "<h3 class='text-primary'>用户名：<font size = '5' color='#D9534F'>" + account .Username+ "</font></h3>" +
                                       "<h3 class='text-primary'>联系方式：<font size = '5' color='#D9534F'>" + account.Telephone + "</font></h3>" +
                                       "</blockquote>" +
                                    "<div class='row'>" +
                                        "<div class='col-md-12'><font size = '5' > 合 计：</font><font size = '7' color='#D9534F'>{7}</ font ></div>" +
                                        "<div class='col-md-12  float-right'>" +
                                        "<input type = 'text' style='display:none' id='cartId' value='{0}'/>" +
                                        "<input type = 'text' style='display:none' id='bookId' value='{6}'/>" +
                                        "<input type = 'text' style='display:none' id='num' value='{5}'/>" +
                                         "<input type = 'text' style='display:none' id='money' value='{7}'/>" +
                                           "<input type = 'button' class='btn btn-danger' id='btnAddOrder' value='提交订单'/>" +
                                        "</div>" +
                                    "</div>" +
                                    "<br/>" +
                                    "<div" +
                                        "<lable class='text-danger' id='errorInfo' value=''></label>" +
                                    "</div>" +
                                "</div>" +
                            "</div>" +
                       " </div>" +
                    "</div>" +
                "</div>", cart.Id, cart.Book.Image, cart.Book.Name, cart.Book.Oneword, cart.Book.Price, cart.Num, cart.Book.Id,cart.Book.Price * cart.Num);
            }
            // 返回给浏览器
            OrderInfor = sbBuilder.ToString();
        }
        else
        {
            EmptyInfo.Text = "请求失败";
            //Response.Redirect("Cart.aspx");
        }
    }

}